package org.hum.scaffold.dubbo.util;

import com.alibaba.dubbo.common.URL;

public class UrlUtil {

	/**
	 * dubbo://localhost:20880/org.hum.scaffold.dubbo.DemoService?application=scaffold&default.timeout=10000000&dubbo=2.0.2&interface=org.hum.scaffold.dubbo.DemoService&methods=sayHello,sayHello2&pid=19632&protocol=dubbo&register.ip=192.168.0.102&serialization=kryo&side=consumer&timeout=10000000&timestamp=1618654669719rg.hum.scaffold.dubbo.DemoService?application=scaffold&default.timeout=10000000&dubbo=2.0.2&interface=org.hum.scaffold.dubbo.DemoService&methods=sayHello,sayHello2&pid=19632&protocol=dubbo&register.ip=192.168.0.102&serialization=kryo&side=consumer&timeout=10000000&timestamp=1618654669719
	 * @param url
	 * @return
	 */
	public static URL toUrl(String url) {
		String protocol = url.split("://")[0];
		String host = url.split("://")[1].split("/")[0].split(":")[0];
		int port = Integer.parseInt(url.split("://")[1].split("/")[0].split(":")[1]);
		URL u = new URL(protocol, host, port);
		String params = url.split("\\?")[1];
		for (String param : params.split("&")) {
			String[] p = param.split("=");
			u = u.addParameter(p[0], p[1]);
		}
		return u;
	}
	
	public static void main(String[] args) {
		String _url = "dubbo://localhost:20880/org.hum.scaffold.dubbo.DemoService?application=scaffold&default.timeout=10000000&dubbo=2.0.2&interface=org.hum.scaffold.dubbo.DemoService&methods=sayHello,sayHello2&pid=19632&protocol=dubbo&register.ip=192.168.0.102&serialization=kryo&side=consumer&timeout=10000000&timestamp=1618654669719rg.hum.scaffold.dubbo.DemoService?application=scaffold&default.timeout=10000000&dubbo=2.0.2&interface=org.hum.scaffold.dubbo.DemoService&methods=sayHello,sayHello2&pid=19632&protocol=dubbo&register.ip=192.168.0.102&serialization=kryo&side=consumer&timeout=10000000&timestamp=1618654669719";
		System.out.println(toUrl(_url).getParameter("application"));
	}
}
